package com.gw.test;

import net.spy.memcached.MemcachedClient;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;

public class testMemcached {
    @Test
    public void testMemcached(){
        try {
            //连接memcached本地服务
            MemcachedClient mcc=new MemcachedClient(new InetSocketAddress("127.0.0.1",11211));
            //输出连接信息
            System.out.println("memcached连接成功！");
            /*//添加缓存
            mcc.add("name",900,"zhangsan");
            //mcc.add("name",900,"lisi"); //缓存中的数据仍为zhangsan
            mcc.set("name",900,"wangwu"); //此时缓存中的数据为wangwu
            //替换缓存中的数据
            mcc.replace("name",900,"chenhaomin"); //此时缓存中的数据为chenhaomin
            //给缓存中key对应的value后面追加数据
            mcc.append("name","黎明");
            //给缓存中key对应的value前面追加数据
            mcc.prepend("name","张学友");*/

            //删除缓存中的数据
            mcc.delete("age");
            //读取缓存
            /*Object o=mcc.get("name");
            System.out.println("缓存中的数据为："+o);*/
            //数值类型：自增，自减
            mcc.add("age",900,"1");
            //mcc.add("age",900,23);
            //自增5
            mcc.incr("age",20);

            //自减5
            mcc.decr("age",5);

            System.out.println(mcc.get("age"));
            //关闭连接
            mcc.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
